<?php

class Ajax extends CI_Controller{

	public function __construct()
	{
		parent::__construct();
		$this->load->spark('ion_auth/2.3.2');
		$this->load->database();
	}

	public function get_unregistered_pupils()
	{

		//is the user logged in?
		if (!$this->ion_auth->logged_in())
		{
			//redirect them back to the page they came from
			echo "All your base are belong to us";
			return;
		}

		$this->load->model('pupils_model');
		$class = trim($_GET['class']);
		$date = trim($_GET['date']);
		$date = date('Y-m-d', strtotime(str_replace('/', '-', $date)));	
		$pupils = $this->pupils_model->get_unregistered_pupils($class,$date);
		echo json_encode($pupils);
	}


	public function get_roominfo()
	{

		//is the user logged in?
		if (!$this->ion_auth->logged_in())
		{
			//redirect them back to the page they came from
			echo "All your base are belong to us";
			return;
		}

		$this->load->model('rooms_model');
		$room = trim($_GET['room']);
		$roominfo = $this->rooms_model->get_rooms($room);
		echo json_encode($roominfo);
	}

	public function get_entries()
	{

		//is the user logged in?
		if (!$this->ion_auth->logged_in())
		{
			//redirect them back to the page they came from
			echo "All your base are belong to us";
			return;
		}

		$this->load->model('entries_model');
		$room = trim($_GET['room']);
		$date = trim($_GET['date']);
		$date = date('Y-m-d', strtotime(str_replace('/', '-', $date)));
		$entries = $this->entries_model->get_room_entries($room,$date);
		echo json_encode($entries);
	}
	
	public function set_entry()
	{

		//is the user logged in?
		if (!$this->ion_auth->logged_in())
		{
			//redirect them back to the page they came from
			echo "All your base are belong to us";
			return;
		}
		$this->load->model('entries_model');
		$pupil_id = trim($_GET['pupil_id']);
		$room = trim($_GET['room']);
		$date = trim($_GET['date']);
		$date = date('Y-m-d', strtotime(str_replace('/', '-', $date)));
		$teacher_id = $this->ion_auth->user()->row()->id;
		$entries = $this->entries_model->set_entry($pupil_id,$room,$teacher_id,$date);
		return;
	}
	
	public function delete_entry()
	{

		//is the user logged in?
		if (!$this->ion_auth->logged_in())
		{
			//redirect them back to the page they came from
			echo "All your base are belong to us";
			return;
		}
		$this->load->model('entries_model');
		$pupil_id = trim($_GET['pupil_id']);
		$room = trim($_GET['room']);
		$date = trim($_GET['date']);
		$date = date('Y-m-d', strtotime(str_replace('/', '-', $date)));
		$teacher_id = $this->ion_auth->user()->row()->id;
		$entries = $this->entries_model->delete_entry($pupil_id,$room,$teacher_id,$date);
	}	

	public function roll_call()
	{

		//is the user logged in?
		if (!$this->ion_auth->logged_in())
		{
			//redirect them back to the page they came from
			echo "All your base are belong to us";
			return;
		}
		$this->load->model('entries_model');
		$pupil_id = trim($_GET['pupil_id']);
		$room = trim($_GET['room']);
		$status = trim($_GET['status']);
		$date = trim($_GET['date']);
		$date = date('Y-m-d', strtotime(str_replace('/', '-', $date)));
		$roll_call_teacher_id = $this->ion_auth->user()->row()->id;
		$entries = $this->entries_model->roll_call($pupil_id,$room,$roll_call_teacher_id,$date,$status);
		return;
	}


}